package com.trolls.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CompositeList extends Composite {
	private VerticalPanel vPanelList = new VerticalPanel();
	private ListBox listBoxItems;
	// Déclaration des panels et boutons
	private ArrayList<Item> items;
	private int NB_ITEM_MAX = 5;
	
	public CompositeList(ArrayList<Item> listItems, int nb_item_max) {
		this(listItems);
		NB_ITEM_MAX = nb_item_max;
	}
	public CompositeList(ArrayList<Item> listItems) {
		items = listItems;
		VerticalPanel vPanelPrincipal = new VerticalPanel();
		vPanelPrincipal.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		HTML titreListe = new HTML("Liste des Trolls");
		titreListe.setStyleName("titre3 titreListe");
		vPanelPrincipal.add(titreListe);
		vPanelList = new VerticalPanel();
		vPanelList.setStyleName("listTxt");

		//On affiche la liste d'Item
		listBoxItems = new ListBox();
		listBoxItems.getElement().setAttribute("size", String.valueOf(NB_ITEM_MAX));
		listBoxItems.setStyleName("listBoxItems");
		for (int i = 0; i < listItems.size(); i++) {
			listBoxItems.addItem(listItems.get(i).getName(),String.valueOf(i));
		}
		listBoxItems.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int numItem = Integer.parseInt(listBoxItems.getValue(listBoxItems.getSelectedIndex()));
				items.get(numItem).afficherItem(LaTerreDesTrolls.get().getContent());

			}
		});

		vPanelPrincipal.add(listBoxItems);

		this.initWidget(vPanelPrincipal);
	}

}
